package demo1;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    //由于tcpsocket里面会记录 服务器的相关信息 所以这里不用手动记录了
    Socket socket;

    public TcpEchoClient(String serverIp, int port) throws IOException {
        socket = new Socket(serverIp, port);
    }

    public void start() throws IOException {
        System.out.println("客户端启动");
        System.out.println("输入要发送的请求");
        try (InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream()) {
            while (true) {
                Scanner scanner = new Scanner(inputStream);
                Scanner scanner1 = new Scanner(System.in);
                String request = scanner1.next();
                PrintWriter writer = new PrintWriter(outputStream);
                writer.println(request);
                writer.flush();
                String response = scanner.next();
                System.out.println("响应: " + response);
            }
        }
    }

    static void main() throws IOException {
        TcpEchoClient client = new TcpEchoClient("127.0.0.1", 9090);
        client.start();
    }


}
